package com.loveazure.ui;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import com.loveazure.bll.Handler;
import com.loveazure.bll.LearnedProgress;
import com.loveazure.bll.WordProvider;
import com.loveazure.bll.algorithm.SM2;
import com.loveazure.bll.algorithm.WordItem;
import com.loveazure.ui.action.ImpressNoAction;
import com.loveazure.ui.action.ImpressYesAction;
import com.loveazure.ui.action.NextAction;
import com.loveazure.ui.action.ResultRightAction;
import com.loveazure.ui.action.ResultWrongAction;
import com.loveazure.ui.action.ReturnAction;
import com.loveazure.ui.action.SoundAction;

public class LearnPanel extends IPanel {

	/* learnPanel所需的action/button的状态 */
	public enum action_status {
		impress, next, result
	}

	/* 显示单词拼写的控件 */
	private JLabel wordLbl;;

	/* 显示音标的控件 */
	private JLabel pronLbl;

	/* 显示单词解释的控件 */
	private JTextArea defArea;

	private JButton trueBtn;

	private JButton falseBtn;

	/* 返回按钮 */
	private JButton returnBtn;

	/* 发音按钮 */
	private JButton soundBtn;

	/* 学习进度 */
	private JLabel learningProgress;

	private JTextArea centArea;

	/* 当前WordItem */
	private WordItem word;

	/* Handler实例 */
	private Handler handler;

	private JPanel rightPanel;

	public LearnPanel() {
		super();

	}

	/**
	 * 初始化learnPanel内的控件
	 */
	@Override
	public void iComponent() {
		GridBagLayout gbl = new GridBagLayout();

		gbl.rowHeights = new int[] { 40, 40, 40, 40 };

		rightPanel = new JPanel(gbl);
		rightPanel.setBorder(BorderFactory.createTitledBorder("操作"));

		learningProgress = new JLabel();

		/* 实例化显示单词的控件 */
		wordLbl = new JLabel();
		wordLbl.setFont(wordLbl.getFont().deriveFont(Font.BOLD, 18.0f));

		pronLbl = new JLabel();

		/* 实例化显示解释的JTextArea控件 */
		defArea = new JTextArea();
		defArea.setEditable(false);
		defArea.setLineWrap(true);

		JScrollPane defPane = new JScrollPane(defArea);
		defArea.setPreferredSize(new Dimension(230, 50));

		trueBtn = new JButton();
		rightPanel.add(trueBtn, new GBC(0, 0, 1, 1));

		falseBtn = new JButton();
		rightPanel.add(falseBtn, new GBC(0, 1, 1, 1));

		soundBtn = new JButton(new SoundAction(this));
		rightPanel.add(soundBtn, new GBC(0, 2, 1, 1));

		returnBtn = new JButton(new ReturnAction(this));
		rightPanel.add(returnBtn, new GBC(0, 3, 1, 1));

		centArea = new JTextArea();
		centArea.setEditable(false);
		centArea.setLineWrap(true);
		centArea.setWrapStyleWord(true);

		JScrollPane centPane = new JScrollPane(centArea);
		centPane.setPreferredSize(new Dimension(230, 100));

		/* 将所有的控件添加到 learnPanel 中 */
		add(learningProgress, new GBC(0, 0, 3, 1));
		add(wordLbl, new GBC(0, 1, 2, 1).setInsets(5));
		add(pronLbl, new GBC(0, 2, 2, 1).setInsets(5));
		add(defPane, new GBC(0, 3, 2, 1).setFill(GBC.BOTH).setInsets(5));
		add(centPane, new GBC(0, 4, 2, 1).setFill(GBC.BOTH).setInsets(5));
		add(rightPanel, new GBC(2, 2, 1, 3).setFill(GBC.BOTH));
	}

	/**
	 * 初始化数据
	 */
	@Override
	public void iInit() {
		TimerTask refreshTask = new TimerTask() {
			@Override
			public void run() {
				LearnedProgress progress = handler.getProgress();
				progress.increaseSpent();
				learningProgress.setText(progress.toString());
			}
		};

		handler = Handler.getInstance();
		Timer timer = new Timer();
		timer.schedule(refreshTask, 0, 1000);

		handler.setAlgorithm(new SM2(new WordProvider()));
		switchStatus(action_status.impress);
		displayNewWord();
		revalidate();

		// registerKey(trueBtn, "I");
		// registerKey(falseBtn, "O");
		// registerKey(soundBtn, "P");
		// registerKey(returnBtn, "E");
	}

	/**
	 * 初始化panel的layout
	 */
	@Override
	public void iLayout() {

		GridBagLayout gbl = new GridBagLayout();
		gbl.columnWeights = new double[] { 40, 40, 25 };
		gbl.rowWeights = new double[] { 20, 10, 10, 20, 40 };
		setLayout(gbl);

	}

	/**
	 * 根据状态切换currentActionPanel,并将actionPanel加到learnPanel中
	 * 
	 * @param status
	 */
	public void switchStatus(action_status status) {
		switch (status) {
		case impress:
			trueBtn.setAction(new ImpressYesAction(this));
			falseBtn.setAction(new ImpressNoAction(this));
			falseBtn.setEnabled(true);
			break;
		case next:
			trueBtn.setAction(new NextAction(this));
			falseBtn.setEnabled(false);
			break;
		case result:
			trueBtn.setAction(new ResultRightAction(this));
			falseBtn.setAction(new ResultWrongAction(this));
			falseBtn.setEnabled(true);
			break;
		default:
			break;
		}
	}

	/**
	 * 为JButton注册相应的快捷键
	 * 
	 * @param button
	 * @param key
	 */
	// private void registerKey(JButton button, String key) {
	// System.out.println("action key");
	// final String str = "action";
	// key = key.toUpperCase();
	// button.getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW).put(
	// KeyStroke.getKeyStroke(key), str);
	// button.getActionMap().put(str, button.getAction());
	// }

	public void processWordResult(int result) {
		word.setQuality(result);
		handler.getAlgorithm().computeResult();
		word.setTimes(word.getTimes() + 1);
		word.updateLearnedWord();
		handler.getProgress().increaseTimes();
	}

	public void displayNewWord() {
		word = handler.getAlgorithm().displayWord();
		wordLbl.setText(word.getName());
		pronLbl.setText("[" + word.getPsymbol() + "]");
		defArea.setText("");
		centArea.setText("");
		switchStatus(action_status.impress);
	}

	public void showDef() {
		this.defArea.setText(word.getDefinition());
		this.centArea.setText(word.getCent());
		centArea.setCaretPosition(0);

	}

}
